<template>
  <div>
    <TableSlot :data="list">
      <template v-slot:btn="obj">
        <button @click="edit(obj.row, false)">编辑</button>
      </template>
    </TableSlot>
    <TableSlot :data="list2">
      <template v-slot:btn="obj">
        <button @click="fn(obj.row, true)">查看</button>
        <button @click="del(obj.row)">删除</button>
      </template>
      <!-- <template #btn="obj1">
        {{ obj1.row }}
        <button @click="fn(obj1.row)">查看</button>
        <button @click="del(obj1.row)">删除</button>
      </template> -->
      <!-- <template slot="btn" slot-scope="obj">
        {{ obj.row }}
        <button @click="fn(obj.row)">查看</button>
        <button @click="del(obj.row)">删除</button>
      </template> -->
    </TableSlot>
    <!-- <InfoCom :info="listInfo" :showFlag.sync="flag"></InfoCom> -->
    <InfoCom v-if="flag" @close="close">
      <template v-slot:form>
        <form>
          <p><strong>序号：</strong>{{ listInfo.id }}</p>
          <p>
            <strong>姓名：</strong
            ><input
              :disabled="listInfo.dis"
              type="text"
              v-model="listInfo.name"
            />
          </p>
          <p>
            <strong>年纪：</strong
            ><input
              :disabled="listInfo.dis"
              type="text"
              v-model="listInfo.age"
            />
          </p>
          <p class="btn">
            <button @click="confirm(listInfo.id)" v-if="!listInfo.dis">
              确认</button
            ><button @click="close">取消</button>
          </p>
        </form>
      </template>
    </InfoCom>
    <button @click="add">添加</button>
  </div>
</template>
<script>
import TableSlot from "./slot/TableSlot.vue";
import InfoCom from "./slot/InfoCom.vue";
import _ from "lodash";
export default {
  components: {
    TableSlot,
    InfoCom,
  },
  data() {
    return {
      list: [
        {
          id: 1,
          name: "张小花",
          age: 19,
        },
        {
          id: 2,
          name: "孙大明",
          age: 29,
        },
        {
          id: 3,
          name: "刘德忠",
          age: 39,
        },
      ],
      list2: [
        {
          id: 1,
          name: "赵小云",
          age: 19,
        },
        {
          id: 2,
          name: "刘蓓蓓",
          age: 29,
        },
        {
          id: 3,
          name: "姜肖泰",
          age: 39,
        },
      ],
      flag: false,
      listInfo: {
        id: "",
        name: "",
        age: "",
      },
    };
  },
  created() {},
  computed: {},
  methods: {
    fn(id, b) {
      this.flag = true;
      this.listInfo = this.list2.find((item) => item.id == id);
      this.listInfo.dis = b;
    },
    del(id) {
      this.list2 = this.list2.filter((item) => item.id !== id);
    },
    edit(id, b) {
      this.flag = true;
      this.listInfo = _.cloneDeep(this.list.find((item) => item.id === id));
      this.listInfo.dis = b;
    },
    close() {
      this.flag = false;
      this.listInfo = {
        id: "",
        name: "",
        age: "",
      };
    },
    confirm(id) {
      this.flag = false;
      //       this.list[this.list.findIndex((item) => item.id == id)] = _.cloneDeep(
      //         this.listInfo
      //       );
      //       this.$set(
      //         this.list,
      //         this.list.findIndex((item) => item.id == id),
      //         this.listInfo
      //       );
      const ld = this.list.find((item) => item.id == id);
      if (ld) {
        ld.name = this.listInfo.name;
        ld.age = this.listInfo.age;
        this.listInfo = {};
      } else {
        this.list.push(this.listInfo);
      }
      this.listInfo = {
        id: "",
        name: "",
        age: "",
      };
    },
    add() {
      this.listInfo.id = Date.now();
      this.flag = true;
    },
  },
};
</script>
<style lang='less'  scoped>
body {
  background-color: #b3b3b3;
}
.btn {
  display: flex;
  justify-content: space-between;
  margin-top: 5px;
}
.btn button {
  width: 100px;
}
</style>